<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Commodity;
use App\Model\Commodity_info;


/**
 * Class CommodityController
 * @package App\Http\Controllers\Api
 * @User wuqin
 * @Date 2018/3/29 0029
 * @Time 20:31
 */
class CommodityController extends Controller
{
    /**
     * 方法 : get_commodity_by_classify
     * @User wuqin
     * @DateTime 2018-03-29 20:31
     * @param Request $request
     * @return array
     */
    public function get_commodity_by_classify(Request $request)
    {
        $data = $request->input();
        if (empty($data) || empty($data['classify'])) {
            return return_array(1, '非法参数');
        }
        //获取该分类商品
        $commodityModel = new Commodity();
        $commodity = $commodityModel->get_commodity_by_classify_id((int)$data['classify']);

        if (!empty($commodity)) {
            $infoModel = new Commodity_info();
            foreach ($commodity as $key => $value) {
                $keys = $infoModel->get_info_keys_by_commodity((int)$value['commodity_id']);
                preg_match('/(?<=commodity_info:).*?(?=(:commodity))/', $keys[0], $matches);
                if (empty($matches) || $matches[0] == '0') {
                    unset($commodity[$key]);
                    continue;
                }
                $res = $infoModel->mget_array_redis($keys);
                if (empty($res) || count($res) == 0)
                    unset($commodity[$key]);
                $commodity[$key]['commodity_info_price'] = reset($res)['commodity_info_price'];
            }
        }
        if (empty($commodity)) {
            return return_array(1, '该分类下暂无商品');
        } else {
            return return_array(0, '获取成功', $commodity);
        }
    }
}
